#include<bits/stdc++.h>
using namespace std;
const int NN=1e7+4;
int fac[NN],ifac[NN],P,p2[NN],pn[NN];
int qmi(int a,int b)
{
	int res=1;
	while(b)
	{
		if(b&1)
			res=1ll*res*a%P;
		a=1ll*a*a%P;
		b>>=1;
	}
	return res;
}
int C(int n,int m)
{
	if(m<0||n<m)
		return 0;
	return 1ll*fac[n]*ifac[m]%P*ifac[n-m]%P;
}
int main()
{
	freopen("tree.in","r",stdin);
	freopen("tree.out","w",stdout);
	int n;
	scanf("%d%d",&n,&P);
	fac[0]=p2[0]=pn[0]=1;
	for(int i=1;i<=n;i++)
	{
		fac[i]=1ll*fac[i-1]*i%P;
		p2[i]=2ll*p2[i-1]%P;
		pn[i]=1ll*n*pn[i-1]%P;
	}
	ifac[n]=qmi(fac[n],P-2);
	for(int i=n-1;~i;i--)
		ifac[i]=1ll*ifac[i+1]*(i+1)%P;
	int res=1ll*n*qmi(n,n-2)%P*qmi(2,n-1)%P;
	for(int i=2;i<n;i++)
		res=(res+1ll*i*(i-1)%P*pn[n-i-1]%P*p2[n-i]%P*i%P*fac[i-2]%P*C(n,i)%P)%P;
	res=(res+1ll*n*(n-1)%P*fac[n-2]%P)%P;
	printf("%d",res);
	return 0;
}
